package com.example.demo.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author xin.you
 * @date 2022/2/6
 * @description
 */
public class FactoryProxy {
    private Object target;

    public FactoryProxy(Object target) {
        this.target = target;
    }

    //TODO 给目标生成一个代理对象
    public Object getProxyInstance(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object invoke = method.invoke(target, args);
                return invoke;
            }
        });
    }
}
